<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>5. 操作 operation | 被删的前端游乐场</title>
    <meta name="generator" content="VuePress 1.8.2">
    
    <meta name="description" content="Just playing around">
    
    <link rel="preload" href="/front-end-playground/assets/css/0.styles.6ad2a9ca.css" as="style"><link rel="preload" href="/front-end-playground/assets/js/app.1e2670bf.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/2.38d016d1.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/3.e3f029cb.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/80.9082b5e2.js" as="script">
    <link rel="stylesheet" href="/front-end-playground/assets/css/0.styles.6ad2a9ca.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/front-end-playground/" class="home-link router-link-active"><!----> <span class="site-name">被删的前端游乐场</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端领域</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/front-end-work/" class="nav-link">前端与工作</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端领域</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/front-end-work/" class="nav-link">前端与工作</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>Angular框架解读</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>玩转 Angular</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Cycle.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>D3.js 和 Tree</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Three.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:10px;"><div class="kitty-main" data-v-2b653b36><span class="stand" data-v-2b653b36></span> <div class="cat" data-v-2b653b36><div class="body" data-v-2b653b36></div> <div class="head" data-v-2b653b36><div class="ear" data-v-2b653b36></div> <div class="ear" data-v-2b653b36></div></div> <div class="face" data-v-2b653b36><div class="nose" data-v-2b653b36></div> <div class="whisker-container" data-v-2b653b36><div class="whisker" data-v-2b653b36></div> <div class="whisker" data-v-2b653b36></div></div> <div class="whisker-container" data-v-2b653b36><div class="whisker" data-v-2b653b36></div> <div class="whisker" data-v-2b653b36></div></div></div> <div class="tail-container" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36></div></div></div></div></div></div></div></div></div></div> <p class="sidebar-heading open"><span>BOX2D.js 文档与教程</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/front-end-playground/front-end-others/box2d-tutorial/1-0-basic-catalog.html" class="sidebar-link">1. 基本概念</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/2-0-world.html" class="sidebar-link">2. 物理世界</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/3-0-shape-and-body.html" class="sidebar-link">3. 形状 shape - 刚体 body</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/4-0-joint.html" class="sidebar-link">4. 关节 joint</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/5-0-operation.html" aria-current="page" class="active sidebar-link">5. 操作 operation</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/box2d-tutorial/5-0-operation.html#鼠标获取刚体" class="sidebar-link">鼠标获取刚体</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/box2d-tutorial/5-0-operation.html#获取参与碰撞的刚体" class="sidebar-link">获取参与碰撞的刚体</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/box2d-tutorial/5-0-operation.html#获取刚体的各属性" class="sidebar-link">获取刚体的各属性</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/box2d-tutorial/5-0-operation.html#为刚体设置属性" class="sidebar-link">为刚体设置属性</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/box2d-tutorial/5-0-operation.html#绘制刚体" class="sidebar-link">绘制刚体</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/box2d-tutorial/5-0-operation.html#绘制关节" class="sidebar-link">绘制关节</a></li></ul></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-1-create-and-init-world.html" class="sidebar-link">6-1. 创建世界并初始化</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-2-add-body.html" class="sidebar-link">6-2. 添加刚体</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-3-add-bound.html" class="sidebar-link">6-3. 添加边界</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-4-mouse-operate-body.html" class="sidebar-link">6-4. 鼠标操作刚体</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-5-handle-contact.html" class="sidebar-link">6-5. 处理碰撞刚体</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/7-0-api.html" class="sidebar-link">7. api</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><p>第五章主要介绍操作(operation)。</p> <h2 id="鼠标获取刚体"><a href="#鼠标获取刚体" class="header-anchor">#</a> 鼠标获取刚体</h2> <h3 id="鼠标获取刚体-对刚体进行操作的前提"><a href="#鼠标获取刚体-对刚体进行操作的前提" class="header-anchor">#</a> 鼠标获取刚体--对刚体进行操作的前提</h3> <p>Box2D 中，只要提供一个 AABB 的坐标，b2World 就可以返回一个数组，用于保存所有相交于此 AABB 的形状。</p> <p>所以，为了利用鼠标获取刚体，首先要画出一个小区域，这个区域小到近似于一个点，然后遍历整个世界的形状，判断鼠标点击位置所画出的小区域是否与某个形状重合，如果有重合，再由这个形状得到这个形状所属的刚体。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">GetBodyAtMouse</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">//首先创建一个近似于点的小区域</span>
  <span class="token keyword">var</span> mousePVec <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span>mousedwX<span class="token punctuation">,</span> mousedwY<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">//利用b2Vec2定义一个矢量，用来保存鼠标点击的点</span>
  <span class="token keyword">var</span> aabb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2AABB</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">//利用b2AABB创建一个环境</span>
  aabb<span class="token punctuation">.</span>minVertex<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span>mousePVec<span class="token punctuation">.</span>x <span class="token operator">-</span> <span class="token number">0.001</span><span class="token punctuation">,</span> mousePVec<span class="token punctuation">.</span>y <span class="token operator">-</span> <span class="token number">0.001</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  aabb<span class="token punctuation">.</span>maxVertex<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span>mousePVec<span class="token punctuation">.</span>x <span class="token operator">+</span> <span class="token number">0.001</span><span class="token punctuation">,</span> mousePVec<span class="token punctuation">.</span>y <span class="token operator">+</span> <span class="token number">0.001</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">//设置aabb的左上角及右下角坐标，这里是以鼠标点击位置为中心创建了一个长、宽均为0.002的矩形区域</span>

  <span class="token comment">//然后查询与指定区域有重叠的刚体</span>
  <span class="token keyword">var</span> k_maxCount <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span> <span class="token comment">//设定所要查找形状的数量，注意合理设置其大小，过大会影响运行速度</span>
  <span class="token keyword">var</span> shapes <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//保存查找到的与已知边界盒相交的形状</span>
  <span class="token keyword">var</span> count <span class="token operator">=</span> world<span class="token punctuation">.</span><span class="token function">Query</span><span class="token punctuation">(</span>aabb<span class="token punctuation">,</span> shapes<span class="token punctuation">,</span> k_maxCount<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//在世界中查找与边界盒相交的maxCount个形状，并返回边界盒区域内实际包含的形状的个数</span>

  <span class="token keyword">var</span> findBody <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token comment">//首先设定没有找到物体</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> count<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>shapes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">GetBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">IsStatic</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">//条件假定查找到的形状不是静态刚体，比如墙</span>
      <span class="token keyword">var</span> tShape <span class="token operator">=</span> shapes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//将查找到的形状赋给tShape变量</span>
      <span class="token keyword">var</span> inside <span class="token operator">=</span> tShape<span class="token punctuation">.</span><span class="token function">GetBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//将tShape对应的刚体赋给inside</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>inside<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//如果inside这个刚体存在</span>
        <span class="token comment">//那么返回这个刚体，并跳出遍历</span>
        findBody <span class="token operator">=</span> tShape<span class="token punctuation">.</span><span class="token function">GetBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> findBody<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="获取参与碰撞的刚体"><a href="#获取参与碰撞的刚体" class="header-anchor">#</a> 获取参与碰撞的刚体</h2> <h3 id="m-contactlist"><a href="#m-contactlist" class="header-anchor">#</a> m_contactList</h3> <p>m_contactList 的类型是 b2ContactNode，取得附加在该物体上的碰撞表，所有碰撞都保存在这个列表里。
同 ShapeList 相比，ShapeList 保存的是世界中所有的形状，而 contactlist 保存的是参与碰撞的形状。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">getContactInf</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span>
    <span class="token keyword">var</span> b <span class="token operator">=</span> world<span class="token punctuation">.</span>m_contactList<span class="token punctuation">;</span>
    b<span class="token punctuation">;</span>
    b <span class="token operator">=</span> b<span class="token punctuation">.</span>m_next <span class="token comment">// 遍历contactlist所有世界，直到b不存在，跳出循环</span>
  <span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 将b里的两个刚体分别定义为b1和b2</span>
    <span class="token keyword">var</span> b1 <span class="token operator">=</span> b<span class="token punctuation">.</span>m_shape1<span class="token punctuation">.</span>m_body<span class="token punctuation">;</span>
    <span class="token keyword">var</span> b2 <span class="token operator">=</span> b<span class="token punctuation">.</span>m_shape2<span class="token punctuation">.</span>m_body<span class="token punctuation">;</span>

    <span class="token comment">// 向下执行的条件是b1和b2不同，且不是三面挡板（BodyTop/BodyLeft/BodyRight）</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>
      b1 <span class="token operator">!=</span> Body2 <span class="token operator">&amp;&amp;</span>
      b1 <span class="token operator">!=</span> BodyTop <span class="token operator">&amp;&amp;</span>
      b1 <span class="token operator">!=</span> BodyLeft <span class="token operator">&amp;&amp;</span>
      b1 <span class="token operator">!=</span> BodyRight <span class="token operator">&amp;&amp;</span>
      b2 <span class="token operator">!=</span> Body2 <span class="token operator">&amp;&amp;</span>
      b2 <span class="token operator">!=</span> BodyTop <span class="token operator">&amp;&amp;</span>
      b2 <span class="token operator">!=</span> BodyLeft <span class="token operator">&amp;&amp;</span>
      b2 <span class="token operator">!=</span> BodyRight
    <span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 备注：得到了参与碰撞的刚体，则可以对其中的所有刚体的操作，这个发挥的空间留给游戏开发过程</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="getcontactlist"><a href="#getcontactlist" class="header-anchor">#</a> GetContactList</h3> <p>GetContactList 其实与上面的作用效果一样，而且其判断接触的逻辑思路也是一致的。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> cn <span class="token operator">=</span> world<span class="token punctuation">.</span><span class="token function">GetContactList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> cn <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> cn <span class="token operator">=</span> cn<span class="token punctuation">.</span><span class="token function">GetNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> body1 <span class="token operator">=</span> cn<span class="token punctuation">.</span><span class="token function">GetShape1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">GetBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> body2 <span class="token operator">=</span> cn<span class="token punctuation">.</span><span class="token function">GetShape2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">GetBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 处理判断发生碰撞的两个刚体是不是分别为箱子和墙壁</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>body1 <span class="token operator">==</span> box <span class="token operator">&amp;&amp;</span> body2 <span class="token operator">==</span> wall<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span>body2 <span class="token operator">==</span> box <span class="token operator">&amp;&amp;</span> body1 <span class="token operator">==</span> wall<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 进行逻辑处理</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="获取刚体的各属性"><a href="#获取刚体的各属性" class="header-anchor">#</a> 获取刚体的各属性</h2> <h3 id="body-get属性名"><a href="#body-get属性名" class="header-anchor">#</a> <code>Body.Get属性名()</code></h3> <p>在实际编程中，往往需要得到一些关于刚体的信息、数据等，以便下一步对刚体进行一系列操作。
下面代码中给出了获取某些数据的方法，还有一些设置刚体属性的语句。语法格式一般为 Body.Get 属性名()。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">showAllFuc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">//获得刚体位置</span>
  <span class="token keyword">var</span> OriginPosition <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//利用b2Vec2定义一个起始位置的二维矢量</span>
  OriginPosition <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">GetOriginPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取刚体坐标原点，对称图形的原点即中心点</span>
  <span class="token function">alert</span><span class="token punctuation">(</span>OriginPosition<span class="token punctuation">.</span>x <span class="token operator">+</span> <span class="token string">&quot;,&quot;</span> <span class="token operator">+</span> OriginPosition<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//显示该坐标</span>

  <span class="token keyword">var</span> mass<span class="token punctuation">;</span>
  mass <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">GetMass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取刚体质量</span>

  <span class="token keyword">var</span> inertia<span class="token punctuation">;</span>
  inertia <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">GetInertia</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取惯性，类似力的大小</span>
  <span class="token function">alert</span><span class="token punctuation">(</span>inertia<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">var</span> worldPoint<span class="token punctuation">;</span>
  <span class="token keyword">var</span> localPoint <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  worldPoint <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">GetWorldPoint</span><span class="token punctuation">(</span>localPoint<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//由局部坐标得到世界坐标</span>
  <span class="token function">alert</span><span class="token punctuation">(</span>
    slectBody<span class="token punctuation">.</span><span class="token function">GetCenterPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>x <span class="token operator">+</span> <span class="token string">&quot;,&quot;</span> <span class="token operator">+</span> slectBody<span class="token punctuation">.</span><span class="token function">GetCenterPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>y
  <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//世界坐标=局部坐标+刚体中心点坐标</span>
  <span class="token function">alert</span><span class="token punctuation">(</span>localPoint<span class="token punctuation">.</span>x <span class="token operator">+</span> <span class="token string">&quot;,&quot;</span> <span class="token operator">+</span> localPoint<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">alert</span><span class="token punctuation">(</span>worldPoint<span class="token punctuation">.</span>x <span class="token operator">+</span> <span class="token string">&quot;,&quot;</span> <span class="token operator">+</span> worldPoint<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">var</span> localVector<span class="token punctuation">;</span>
  <span class="token keyword">var</span> worldVector <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span>
    slectBody<span class="token punctuation">.</span><span class="token function">GetCenterPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>x<span class="token punctuation">,</span>
    slectBody<span class="token punctuation">.</span><span class="token function">GetCenterPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>y
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
  localVector <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">GetLocalVector</span><span class="token punctuation">(</span>worldVector<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">alert</span><span class="token punctuation">(</span>worldVector<span class="token punctuation">.</span>x <span class="token operator">+</span> <span class="token string">&quot;,&quot;</span> <span class="token operator">+</span> worldVector<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">alert</span><span class="token punctuation">(</span>localVector<span class="token punctuation">.</span>x <span class="token operator">+</span> <span class="token string">&quot;,&quot;</span> <span class="token operator">+</span> localVector<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">var</span> staticFlage<span class="token punctuation">;</span> <span class="token comment">//定义一个静态标志</span>
  staticFlage <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">IsStatic</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//判断选中物体是否为静止不可移动的刚体，并将结果反馈给静态标志</span>
  <span class="token function">alert</span><span class="token punctuation">(</span>staticFlage<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">var</span> frozenFlage<span class="token punctuation">;</span>
  frozenFlage <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">IsFrozen</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">alert</span><span class="token punctuation">(</span>frozenFlage<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">var</span> sleepingFlage<span class="token punctuation">;</span> <span class="token comment">//定义一个睡眠标志</span>
  sleepingFlage <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">IsSleeping</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//判断选中物体是否已进入睡眠状态，并将结果反 馈给静态标志</span>
  <span class="token function">alert</span><span class="token punctuation">(</span>sleepingFlage<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">var</span> allowSleepingFlage <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">//定义一个允许睡眠的标志，并设定初值为1，即允许睡眠</span>
  slectBody<span class="token punctuation">.</span><span class="token function">AllowSleeping</span><span class="token punctuation">(</span>allowSleepingFlage<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//根据标志判断是否允许睡眠，是则可进入睡眠状态，否则唤醒</span>
  slectBody<span class="token punctuation">.</span><span class="token function">WakeUp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//唤醒睡眠刚体，对于非静止刚体，只有碰撞才能自动唤醒，对其赋予速度、力等值时，需要先行唤醒才有效果</span>

  <span class="token keyword">var</span> shapeList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  shapeList <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">GetShapeList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//数组内保存选中刚体的shapelist；直接用m_shapeList效果一样,列表里是一个个的shape，shape是一个类，其表面形状类型的属性为shape.m_type</span>
  slectBody<span class="token punctuation">.</span><span class="token function">Destroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//在世界里销毁该刚体</span>
  slectBody<span class="token punctuation">.</span><span class="token function">GetNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取下一刚体，在world里刚体储存在m_BodyList里</span>

  <span class="token keyword">var</span> otherBody <span class="token operator">=</span> Body4<span class="token punctuation">;</span>
  <span class="token keyword">var</span> conectJuge<span class="token punctuation">;</span>
  conectJuge <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">IsConnected</span><span class="token punctuation">(</span>otherBody<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//判定选定的刚体是否与另一刚体相连，相连则不碰撞</span>
  <span class="token function">alert</span><span class="token punctuation">(</span>conectJuge<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="为刚体设置属性"><a href="#为刚体设置属性" class="header-anchor">#</a> 为刚体设置属性</h2> <h3 id="设置刚体的属性"><a href="#设置刚体的属性" class="header-anchor">#</a> 设置刚体的属性</h3> <p>创建一个刚体之后，还可以对它进行许多操作，比如设置质量，访问其位置和速度，施加力，以及转换点和向量等。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 设置刚体的属性</span>
<span class="token keyword">var</span> position <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//定义一个点坐标（200，100）</span>
<span class="token keyword">var</span> rotation <span class="token operator">=</span> <span class="token number">0.3</span> <span class="token operator">*</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span><span class="token punctuation">;</span> <span class="token comment">//定义一个弧度值</span>
spclBodyDef<span class="token punctuation">.</span>position<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span>basicX<span class="token punctuation">,</span> basicY<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//设置物体的初始位置置，括号内是矢量坐标</span>
slectBody<span class="token punctuation">.</span><span class="token function">SetCenterPosition</span><span class="token punctuation">(</span>position<span class="token punctuation">,</span> rotation<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//设置选中的刚体的中心位置及其旋转弧度</span>

<span class="token keyword">var</span> CenterPosition <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
CenterPosition <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">GetCenterPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">alert</span><span class="token punctuation">(</span>CenterPosition<span class="token punctuation">.</span>x <span class="token operator">+</span> <span class="token string">&quot;,&quot;</span> <span class="token operator">+</span> CenterPosition<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> RotationMatrix <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Mat22</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
RotationMatrix <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">GetRotationMatrix</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取一个2X2的刚体旋转矩阵</span>
<span class="token function">alert</span><span class="token punctuation">(</span>RotationMatrix<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> LinearVelocity <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//定义一个线速度</span>
slectBody<span class="token punctuation">.</span><span class="token function">WakeUp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//唤醒选中物体，刷新数据</span>
slectBody<span class="token punctuation">.</span><span class="token function">SetLinearVelocity</span><span class="token punctuation">(</span>LinearVelocity<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//设定选中物体的线速度</span>

<span class="token keyword">var</span> LinearVelocity <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
LinearVelocity <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">GetLinearVelocity</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取选中刚体的线速度向量</span>
<span class="token function">alert</span><span class="token punctuation">(</span>LinearVelocity<span class="token punctuation">.</span>x <span class="token operator">+</span> <span class="token string">&quot;,&quot;</span> <span class="token operator">+</span> LinearVelocity<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> AngularVelocity <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">;</span> <span class="token comment">//定义一个角速度</span>
slectBody<span class="token punctuation">.</span><span class="token function">WakeUp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
slectBody<span class="token punctuation">.</span><span class="token function">SetAngularVelocity</span><span class="token punctuation">(</span>AngularVelocity<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//设定选中物体的角速度</span>

<span class="token keyword">var</span> AngularVelocity<span class="token punctuation">;</span>
AngularVelocity <span class="token operator">=</span> slectBody<span class="token punctuation">.</span><span class="token function">GetAngularVelocity</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取选中刚体的角速度向量</span>
<span class="token function">alert</span><span class="token punctuation">(</span>AngularVelocity<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> force <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span><span class="token number">10000</span><span class="token punctuation">,</span> <span class="token number">1000000000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//定义一个力的大小及方向</span>
<span class="token keyword">var</span> point <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span>
  slectBody<span class="token punctuation">.</span><span class="token function">GetCenterPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>x<span class="token punctuation">,</span>
  slectBody<span class="token punctuation">.</span><span class="token function">GetCenterPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>y
<span class="token punctuation">)</span><span class="token punctuation">;</span>
slectBody<span class="token punctuation">.</span><span class="token function">WakeUp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//在指定点给物体施加一个已知大小与方向的力</span>
<span class="token comment">//施加一个力,需要很大才有效果，不如直接给定线速度，一般给力据F=ma给定</span>
slectBody<span class="token punctuation">.</span><span class="token function">ApplyForce</span><span class="token punctuation">(</span>force<span class="token punctuation">,</span> point<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">alert</span><span class="token punctuation">(</span>slectBody<span class="token punctuation">.</span>m_linearVelocity<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> torque <span class="token operator">=</span> <span class="token number">100000000000</span><span class="token punctuation">;</span> <span class="token comment">//定义一个力矩</span>
slectBody<span class="token punctuation">.</span><span class="token function">WakeUp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
slectBody<span class="token punctuation">.</span><span class="token function">ApplyTorque</span><span class="token punctuation">(</span>torque<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//设定选定物体的力矩，数值较大，可以通过设定角速度大小实现相同功能</span>

<span class="token keyword">var</span> impulse <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">100000000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//定义一个物体的冲量</span>
<span class="token keyword">var</span> point <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span>
  slectBody<span class="token punctuation">.</span><span class="token function">GetCenterPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>x<span class="token punctuation">,</span>
  slectBody<span class="token punctuation">.</span><span class="token function">GetCenterPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>y
<span class="token punctuation">)</span><span class="token punctuation">;</span>
slectBody<span class="token punctuation">.</span><span class="token function">WakeUp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//在指定点给物体施加一个已知大小与方向的冲量，一般在初始的时候给定，以便决定初始运动</span>
slectBody<span class="token punctuation">.</span><span class="token function">ApplyImpulse</span><span class="token punctuation">(</span>impulse<span class="token punctuation">,</span> point<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="绘制刚体"><a href="#绘制刚体" class="header-anchor">#</a> 绘制刚体</h2> <hr> <h3 id="绘制刚体图片"><a href="#绘制刚体图片" class="header-anchor">#</a> 绘制刚体图片</h3> <ol><li>在页面中加入图片，并捆绑 id</li></ol> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>img</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box<span class="token punctuation">&quot;</span></span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>images/box.png<span class="token punctuation">&quot;</span></span> <span class="token punctuation">/&gt;</span></span>
</code></pre></div><ol start="2"><li>设置形状定义的 userData 为图片 id</li></ol> <div class="language-javascript extra-class"><pre class="language-javascript"><code>boxSd<span class="token punctuation">.</span>userData <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">&quot;box&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>在绘制世界时，将图片信息绘制进去</li></ol> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 绘画世界</span>
<span class="token keyword">function</span> <span class="token function">drawWorld</span><span class="token punctuation">(</span><span class="token parameter">world<span class="token punctuation">,</span> context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> b <span class="token operator">=</span> world<span class="token punctuation">.</span>m_bodyList<span class="token punctuation">;</span> b <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> b <span class="token operator">=</span> b<span class="token punctuation">.</span>m_next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> s <span class="token operator">=</span> b<span class="token punctuation">.</span><span class="token function">GetShapeList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> s <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> s <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">GetNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">GetUserData</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token keyword">undefined</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 使用数据包括图片</span>
        <span class="token keyword">var</span> img <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">GetUserData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 图片的长和宽</span>
        <span class="token keyword">var</span> x <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">GetPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>x<span class="token punctuation">;</span>
        <span class="token keyword">var</span> y <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">GetPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>y<span class="token punctuation">;</span>
        <span class="token keyword">var</span> topleftX <span class="token operator">=</span> <span class="token operator">-</span><span class="token function">$</span><span class="token punctuation">(</span>img<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">width</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> topleftY <span class="token operator">=</span> <span class="token operator">-</span><span class="token function">$</span><span class="token punctuation">(</span>img<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">height</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>

        context<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">translate</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">rotate</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">GetBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">GetRotation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">drawImage</span><span class="token punctuation">(</span>img<span class="token punctuation">,</span> topleftX<span class="token punctuation">,</span> topleftY<span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">restore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
      <span class="token function">drawShape</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> context<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>注意，以上的方法只试用于圆形和矩形，多边形待验证。</p> <h3 id="绘制刚体形状"><a href="#绘制刚体形状" class="header-anchor">#</a> 绘制刚体形状</h3> <p>有时候我们需要将刚体和关节的形状绘制出来：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 从draw_world.js里面引用的绘画功能</span>
<span class="token keyword">function</span> <span class="token function">drawShape</span><span class="token punctuation">(</span><span class="token parameter">shape<span class="token punctuation">,</span> context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  context<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> <span class="token string">&quot;#003300&quot;</span><span class="token punctuation">;</span>
  context<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">switch</span> <span class="token punctuation">(</span>shape<span class="token punctuation">.</span>m_type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 绘制圆</span>
    <span class="token keyword">case</span> b2Shape<span class="token punctuation">.</span>e_circleShape<span class="token operator">:</span>
      <span class="token keyword">var</span> circle <span class="token operator">=</span> shape<span class="token punctuation">;</span>
      <span class="token keyword">var</span> pos <span class="token operator">=</span> circle<span class="token punctuation">.</span>m_position<span class="token punctuation">;</span>
      <span class="token keyword">var</span> r <span class="token operator">=</span> circle<span class="token punctuation">.</span>m_radius<span class="token punctuation">;</span>
      <span class="token keyword">var</span> segments <span class="token operator">=</span> <span class="token number">16.0</span><span class="token punctuation">;</span>
      <span class="token keyword">var</span> theta <span class="token operator">=</span> <span class="token number">0.0</span><span class="token punctuation">;</span>
      <span class="token keyword">var</span> dtheta <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">2.0</span> <span class="token operator">*</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span><span class="token punctuation">)</span> <span class="token operator">/</span> segments<span class="token punctuation">;</span>
      <span class="token comment">// 画圆圈</span>
      context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>pos<span class="token punctuation">.</span>x <span class="token operator">+</span> r<span class="token punctuation">,</span> pos<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> segments<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span>r <span class="token operator">*</span> Math<span class="token punctuation">.</span><span class="token function">cos</span><span class="token punctuation">(</span>theta<span class="token punctuation">)</span><span class="token punctuation">,</span> r <span class="token operator">*</span> Math<span class="token punctuation">.</span><span class="token function">sin</span><span class="token punctuation">(</span>theta<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> v <span class="token operator">=</span> b2Math<span class="token punctuation">.</span><span class="token function">AddVV</span><span class="token punctuation">(</span>pos<span class="token punctuation">,</span> d<span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>v<span class="token punctuation">.</span>x<span class="token punctuation">,</span> v<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
        theta <span class="token operator">+=</span> dtheta<span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
      context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>pos<span class="token punctuation">.</span>x <span class="token operator">+</span> r<span class="token punctuation">,</span> pos<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>

      <span class="token comment">// 画半径</span>
      context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>pos<span class="token punctuation">.</span>x<span class="token punctuation">,</span> pos<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">var</span> ax <span class="token operator">=</span> circle<span class="token punctuation">.</span>m_R<span class="token punctuation">.</span>col1<span class="token punctuation">;</span>
      <span class="token keyword">var</span> pos2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span>pos<span class="token punctuation">.</span>x <span class="token operator">+</span> r <span class="token operator">*</span> ax<span class="token punctuation">.</span>x<span class="token punctuation">,</span> pos<span class="token punctuation">.</span>y <span class="token operator">+</span> r <span class="token operator">*</span> ax<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>pos2<span class="token punctuation">.</span>x<span class="token punctuation">,</span> pos2<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token comment">// 绘制多边形</span>
    <span class="token keyword">case</span> b2Shape<span class="token punctuation">.</span>e_polyShape<span class="token operator">:</span>
      <span class="token keyword">var</span> poly <span class="token operator">=</span> shape<span class="token punctuation">;</span>
      <span class="token keyword">var</span> tV <span class="token operator">=</span> b2Math<span class="token punctuation">.</span><span class="token function">AddVV</span><span class="token punctuation">(</span>
        poly<span class="token punctuation">.</span>m_position<span class="token punctuation">,</span>
        b2Math<span class="token punctuation">.</span><span class="token function">b2MulMV</span><span class="token punctuation">(</span>poly<span class="token punctuation">.</span>m_R<span class="token punctuation">,</span> poly<span class="token punctuation">.</span>m_vertices<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
      <span class="token punctuation">)</span><span class="token punctuation">;</span>
      context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>tV<span class="token punctuation">.</span>x<span class="token punctuation">,</span> tV<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> poly<span class="token punctuation">.</span>m_vertexCount<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> v <span class="token operator">=</span> b2Math<span class="token punctuation">.</span><span class="token function">AddVV</span><span class="token punctuation">(</span>
          poly<span class="token punctuation">.</span>m_position<span class="token punctuation">,</span>
          b2Math<span class="token punctuation">.</span><span class="token function">b2MulMV</span><span class="token punctuation">(</span>poly<span class="token punctuation">.</span>m_R<span class="token punctuation">,</span> poly<span class="token punctuation">.</span>m_vertices<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>v<span class="token punctuation">.</span>x<span class="token punctuation">,</span> v<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
      context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>tV<span class="token punctuation">.</span>x<span class="token punctuation">,</span> tV<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">break</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  context<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="绘制关节"><a href="#绘制关节" class="header-anchor">#</a> 绘制关节</h2> <h3 id="绘制关节-2"><a href="#绘制关节-2" class="header-anchor">#</a> 绘制关节</h3> <ul><li>在绘制世界时加入关节的判断和绘制调用</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">drawWorld</span><span class="token punctuation">(</span><span class="token parameter">world<span class="token punctuation">,</span> context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> j <span class="token operator">=</span> world<span class="token punctuation">.</span>m_jointList<span class="token punctuation">;</span> j<span class="token punctuation">;</span> j <span class="token operator">=</span> j<span class="token punctuation">.</span>m_next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 绘制关节</span>
    <span class="token function">drawJoint</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> context<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> b <span class="token operator">=</span> world<span class="token punctuation">.</span>m_bodyList<span class="token punctuation">;</span> b<span class="token punctuation">;</span> b <span class="token operator">=</span> b<span class="token punctuation">.</span>m_next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> s <span class="token operator">=</span> b<span class="token punctuation">.</span><span class="token function">GetShapeList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> s <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> s <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">GetNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// 绘制刚体形状</span>
      <span class="token function">drawShape</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> context<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li>绘制关节</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">drawJoint</span><span class="token punctuation">(</span><span class="token parameter">joint<span class="token punctuation">,</span> context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> b1 <span class="token operator">=</span> joint<span class="token punctuation">.</span>m_body1<span class="token punctuation">;</span>
  <span class="token keyword">var</span> b2 <span class="token operator">=</span> joint<span class="token punctuation">.</span>m_body2<span class="token punctuation">;</span>
  <span class="token keyword">var</span> x1 <span class="token operator">=</span> b1<span class="token punctuation">.</span>m_position<span class="token punctuation">;</span>
  <span class="token keyword">var</span> x2 <span class="token operator">=</span> b2<span class="token punctuation">.</span>m_position<span class="token punctuation">;</span>
  <span class="token keyword">var</span> p1 <span class="token operator">=</span> joint<span class="token punctuation">.</span><span class="token function">GetAnchor1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> p2 <span class="token operator">=</span> joint<span class="token punctuation">.</span><span class="token function">GetAnchor2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  context<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> <span class="token string">&quot;#00eeee&quot;</span><span class="token punctuation">;</span>
  context<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">switch</span> <span class="token punctuation">(</span>joint<span class="token punctuation">.</span>m_type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 绘制距离关节</span>
    <span class="token keyword">case</span> b2Joint<span class="token punctuation">.</span>e_distanceJoint<span class="token operator">:</span>
      context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>p1<span class="token punctuation">.</span>x<span class="token punctuation">,</span> p1<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>p2<span class="token punctuation">.</span>x<span class="token punctuation">,</span> p2<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">break</span><span class="token punctuation">;</span>

    <span class="token keyword">case</span> b2Joint<span class="token punctuation">.</span>e_pulleyJoint<span class="token operator">:</span>
      <span class="token comment">// TODO</span>
      <span class="token keyword">break</span><span class="token punctuation">;</span>

    <span class="token keyword">default</span><span class="token operator">:</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>b1 <span class="token operator">==</span> world<span class="token punctuation">.</span>m_groundBody<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>p1<span class="token punctuation">.</span>x<span class="token punctuation">,</span> p1<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>x2<span class="token punctuation">.</span>x<span class="token punctuation">,</span> x2<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>b2 <span class="token operator">==</span> world<span class="token punctuation">.</span>m_groundBody<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>p1<span class="token punctuation">.</span>x<span class="token punctuation">,</span> p1<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>x1<span class="token punctuation">.</span>x<span class="token punctuation">,</span> x1<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>x1<span class="token punctuation">.</span>x<span class="token punctuation">,</span> x1<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>p1<span class="token punctuation">.</span>x<span class="token punctuation">,</span> p1<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>x2<span class="token punctuation">.</span>x<span class="token punctuation">,</span> x2<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>p2<span class="token punctuation">.</span>x<span class="token punctuation">,</span> p2<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
      <span class="token keyword">break</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  context<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div></div> <!----> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/godbasin/front-end-playground/edit/sourcecode/docs/front-end-others/box2d-tutorial/5-0-operation.md" target="_blank" rel="noopener noreferrer">帮阿猪改善此页面！</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----> <blockquote>部分文章中使用了一些网站的截图，如果涉及侵权，请告诉我删一下谢谢~</blockquote> <div style="margin-top:30px;"><div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-sm-0 el-col-md-2 el-col-lg-4" style="padding-left:10px;padding-right:10px;display:block;"><div style="width:1px;height:1px;"></div></div> <div class="el-col el-col-24 el-col-sm-24 el-col-md-18 el-col-lg-16" style="padding-left:10px;padding-right:10px;"><div class="el-card box-card is-always-shadow"><div class="el-card__header"><div class="clearfix"><span>温馨提示喵</span></div></div><div class="el-card__body"> <div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="el-image"><div class="image-slot"><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/loading.gif" style="width:100%;"></div><!----></div></div> <div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="copyright-text"><div>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</div> <div>出处：被删的前端游乐场</div> <div>作者：<a href="https://github.com/godbasin" target="_blank">被删</a></div></div></div></div></div></div></div></div></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/front-end-playground/front-end-others/box2d-tutorial/4-0-joint.html" class="prev">
          4. 关节 joint
        </a></span> <span class="next"><a href="/front-end-playground/front-end-others/box2d-tutorial/6-1-create-and-init-world.html">
          6-1. 创建世界并初始化
        </a>
        →
      </span></p></div>  <div class="gitalk-container theme-default-content"><div id="gitalk-container" class="content"></div></div></main> <div id="kitty-container"><span><div role="tooltip" id="el-popover-5674" aria-hidden="true" class="el-popover el-popper" style="width:undefinedpx;display:none;"><!----><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" class="image"> <div class="text">牡羊猪的猫粮罐</div> </div><span class="el-popover__reference-wrapper"><div id="kitty" style="background:url(https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/kitty1.svg);"></div></span></span> <div class="el-dialog__wrapper" style="display:none;"><div role="dialog" aria-modal="true" aria-label="牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）" class="el-dialog" style="margin-top:15vh;"><div class="el-dialog__header"><span class="el-dialog__title">牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）</span><button type="button" aria-label="Close" class="el-dialog__headerbtn"><i class="el-dialog__close el-icon el-icon-close"></i></button></div><!----><!----></div></div></div></div><div class="global-ui"></div></div>
    <script src="/front-end-playground/assets/js/app.1e2670bf.js" defer></script><script src="/front-end-playground/assets/js/2.38d016d1.js" defer></script><script src="/front-end-playground/assets/js/3.e3f029cb.js" defer></script><script src="/front-end-playground/assets/js/80.9082b5e2.js" defer></script>
  </body>
</html>
